
const dir="./plugins/HalfIsland_Dev";//目录
const config="./plugins/HalfIsland_Dev/configs.json";//配置文件
let data=null;
let cfg=null;
const banner=`
    _   _       _  __ _____    _                 _  ______               _             
   | | | |     | |/ _|_   _|  | |               | | |  _  \\             | |            
   | |_| | __ _| | |_  | | ___| | __ _ _ __   __| | | | | |_____   _____| | ___  _ __  
   |  _  |/ _\` | |  _| | |/ __| |/ _\` | '_ \\ / _\` | | | | / _ \\ \\ / / _ \\ |/ _ \\| '_ \\ 
   | | | | (_| | | |  _| |\\__ \\ | (_| | | | | (_| | | |/ /  __/\\ V /  __/ | (_) | |_) |
   \\_| |_/\\__,_|_|_|  \\___/___/_|\\__,_|_| |_|\\__,_| |___/ \\___| \\_/ \\___|_|\\___/| .__/ 
                                                                                | |    
                                                                                |_|    `;

function init(){
                                                                                        
    if(!file.exists(dir)){
        file.mkdir(dir);
    }

    if(!file.exists(config)){
        let cfg={
            source:0,

            TenCent:{
                key:""
            },

            GaoDe:{
                key:""
            }
        }
           
        file.writeTo(config,JSON.stringify(cfg));
    }

    cfg=file.readFrom(config);

    if(cfg){
        data=JSON.parse(cfg);
    }
        
    log(banner);
    logger.info("[HalfIsland]version:1.0.1");
    logger.info("[HalfIsland]Author:Stranger");
}

function titleAddress(player){//显示地理位置

    let pos=player.pos;

    if(pos.dimid===0)
    {
        let coordinate=toLongitudeAndLatitude(pos.x,pos.z);
        let lot=coordinate.lot.toString();
        let lat=coordinate.lat.toString();

        getAddress(lot,lat,player,data.source);//获取地理位置信息
    }
    else{
        playerTell("仅支持主世界定位服务",player);
    }
}


function toLongitudeAndLatitude(x,y){//转换成真实的经纬度

    let coordinate={
        lot:0,
        lat:0,
    }

    if(!x&&!y)
        return coordinate;
    
    y=-y;// y坐标是反的

    coordinate=compute(x,y);
        
    return  coordinate;
}

function compute(x,y){//粗算
    return {lot:x/60.00000,lat:y/60.00000};
}


function getContinent(lot,lat){
    let continent="";
    if (lot>=56.60156&&lot<=178.59375&&lat>=-9.6224&&lat<=81.387)
        continent="亚洲";
    else if(lot<=56.6015&&lot>=-25.1367&&lat<=79.7499&&lat>=37.02009)
        continent="欧洲";
    else if (lot>=-17.929&&lot<=11.1784&&lat>=-35.6037187&&lat<=37.02009)
        continent="非洲";
    else if (lot>=-86.132812&&lot<=-31.8164&&lat<=14.43468&&lat>=-55.3791104)
        continent="南美洲";
    else if (lot<=-49.74693&&lot>=-168.222&&lat<=83.174035&&lat>=14.43468)
        continent="北美洲";
    else if (lot>=110.74218&&lot<=181.582031&&lat<=-8.928487&&lat>=-43.707593)
        continent="大洋洲";
    else if (lat<=-59.623325)
        continent="南极洲";
    else
        continent="其他区域";

    return continent;
}

function getAddress(lot,lat,player,source){//获取地理位置信息

    let url=getUrl(lot,lat,source);

    network.httpGet(url,function(status,result){

      if(status===200){
       
        let msg="";
        let res=JSON.parse(result);
        logger.info(res);

        if(res.regeocode||res.result)
            msg=source===0?GaoDeApi(res,lot,lat,player):TenCentApi(res,lot,lat,player);
        else
            msg="全球位置信息请求失败";

        if(!msg)    
              msg="全球位置信息请求失败"

        playerTell(msg,player,source);

      }  
        
    })
}

function getUrl(lot,lat,source){
    return source===0?`https://restapi.amap.com/v3/geocode/regeo?key=${data.GaoDe.key}&location=${lot},${lat}`:
    `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lot}&key=${data.TenCent.key}`;
}

function GaoDeApi(res,lot,lat,player){//高德API

    let msg="";
        
    let address=res.regeocode.addressComponent;

    let country=address.country;//地址信息
    let prov=address.province;
    let cities=address.city;
    let district=address.district;
    let continent=getContinent(parseFloat(lot),parseFloat(lat));

    if(country.toString()==="")
        msg=`当前地理位置为:${continent} 国外 纬度${lot} 经度${lat}`;
    else{
        msg=`当前地理位置为:${continent} ${country} ${prov} ${cities}${district}`;
    }
    
    return msg;
}
function TenCentApi(res,lot,lat,player){//腾讯API
    let msg="";
    if(!res.result&&data.source===1){//数据源为腾讯且为无效数据
        if(data.GaoDe.key!==""){
            getAddress(lot,lat,player,0);
            return;
        }else{
            return;
        }
           
    }

    let address=res.result.address_component;
    let continent=getContinent(parseFloat(lot),parseFloat(lat));
    let nation=address.nation;

    if(nation==="中国"){
        let prov=address.province;
        let city=address.city;
        let district=address.district;

        msg=`当前地理位置为:${continent} ${nation} ${prov} ${city} ${district}`;
    }else{
        msg=`当前地理位置为:${continent} ${res.result.address}`;
    }
    
    return msg;
}

function playerTell(msg,player,source){
    player.tell(`§6[${source===0?"高德地图":"腾讯地图"}] §3${msg}`);
}

function selectForWin10(player){//避免win10玩家出现的多次触发

    player.addTag("location");

    setInterval(()=>{
        player.removeTag("location");
    },200);
}


mc.listen("onJoin",function(player){//进服显示地理位置
    if(player){
        titleAddress(player);
    }
})

mc.listen("onLeft",function(player){
    if(player){
        player.removeTag("location");
    }
})

mc.listen("onUseItemOn",function(player,item,block,side){
    if(player&&item&&block&&side){
        if(item.type==="minecraft:compass"){
            if(!player.hasTag("location")){
                titleAddress(player);
                selectForWin10(player);
            }
                
        }
    }
})


init();